//
//  PlaformMsgViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/17.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "PlatformMsgCell.h"

#import "PlatformMsgDetailViewController.h"
#import "PlaformMsgViewController.h"

@interface PlaformMsgViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;

@property (nonatomic, strong) NSMutableArray <CommonMessageModel *>*dataArray;
@end

@implementation PlaformMsgViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self refreshParam];
    [self request_sysMsg];
}

- (void)refreshParam {
    self.pageNum = 1;
    self.pageSize = 20;
}

- (void)initView {
    
    self.title = @"系统通知";

    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 170;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PlatformMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlatformMsgCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.dataArray[indexPath.row].isRead = 1;
    [self.tableview reloadData];
    
    PlatformMsgDetailViewController *vc = [[PlatformMsgDetailViewController alloc] init];
    vc.snId = self.dataArray[indexPath.row].snId;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"PlatformMsgCell" bundle:nil] forCellReuseIdentifier:@"PlatformMsgCellID"];
        
        WEAKSELF
        _tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            [weakSelf refreshParam];
            [weakSelf request_sysMsg];
        }];

        _tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            weakSelf.pageNum ++;
            [weakSelf request_sysMsg];
        }];
    }
    return _tableview;
}

- (NSMutableArray<CommonMessageModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}
//

- (void)request_sysMsg {
    
    NSMutableDictionary *dic = @{}.mutableCopy;

    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [dic setValue:@(self.pageNum) forKey:@"pageNum"];
    [dic setValue:@(self.pageSize) forKey:@"pageSize"];
    
    NSMutableDictionary *t = @{}.mutableCopy;
//    [t setValue:@(self.type) forKey:@"status"];
    [t setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [t setValue:@"1" forKey:@"receiveType"];

    [dic setValue:t forKey:@"t"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_SysMessage];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在获取系统消息"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];

        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                
                NSArray *list = [resultDic valueForKey:@"list"];
                
                if ([list isKindOfClass:[NSArray class]]) {
                    
                    if (weakSelf.pageNum == 1) {
                        weakSelf.dataArray = @[].mutableCopy;
                    }
//
                    for (int i=0; i < list.count; i++) {
                        NSDictionary *listDic = list[i];
                        CommonMessageModel *msgModel = [CommonMessageModel modelWithDictionary:listDic];
                        [weakSelf.dataArray addObject:msgModel];
                    }
                    
                    if (weakSelf.dataArray.count == 0) {
                        
                        if (weakSelf.tableview) {
                            [[TTNoResultManager shareManager] showNoResultWithStateInView:weakSelf.tableview errorCode:kTTViewState_NoMsg btnTitle:@"" didPressButton:^{}];
                        }
                    }
                    
                    [weakSelf.tableview reloadData];
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

@end
